#include<stdio.h>
#include<malloc.h>
#include<string.h>

#include <signal_link_list.h>

/*创建节点*/
static Link_list_t* Create_Node(ElemType *elem)
{
    Link_list_t *node = (Link_list_t *)malloc(sizeof(Link_list_t));
    if (!node)
    {
        return NULL;
    }
    
    node->data = *elem;
    node->next = NULL;
    return node;
}   

/*删除节点*/
static void Delete_Node(Link_list_t *node)
{
    free(node);
}

/*用于打印ElemType内容*/
static void Print_Elem(const ElemType *elem)
{
    printf(" %s | %s | %0.1f  |\n",elem->token,elem->name,elem->price);
}

/*初始化链表*/
Status_t Init_List(Link_list_t **list)
{
    *list = (Link_list_t *)malloc(sizeof(Link_list_t));
    if ((!*list))
    {
        return  ERROR;
    }

    (*list)->next = NULL;
    return OK;
}

/*销毁链表*/
void Destroy_List(Link_list_t **list)
{
    Link_list_t *head = (*list)->next;/*首元节点*/
    while (head)
    {
        Link_list_t *current_node = head->next;
        Delete_Node(head);/*释放所有节点*/
        head = current_node;
    }
    free(*list);
    *list = NULL;
}

/*打印链表*/
void Print_List(Link_list_t *list)
{
    printf("\n| order |    token   |       name     | price |\n");
    if (!list)
    {
        return;
    }
    
    Link_list_t *temp = list->next;
    int order = 0;
    while (temp)
    {
        order ++;
        printf("|   %d   |",order);
        Print_Elem(&(temp->data));
        temp = temp->next;
    }
}

/*获取当前节点个数*/
int get_Node_Num(Link_list_t *list)
{
    if (!list)
    {
        return -1;
    }
    
    int count = 0;
    Link_list_t *temp = list->next;
    while (temp)
    {
        count ++;
        temp = temp->next;
    }
    
    return count;
}

/*尾部插入节点*/
Link_list_t* Push_Back(Link_list_t *list,ElemType *elem)
{
    if (list == NULL)
    {
        perror("list == NULL");
        return NULL;
    }
    Link_list_t *node = Create_Node(elem);/*创建节点*/
    if (node ==NULL)
    {
        perror("node == NULL");
        return list;
    }
    
    Link_list_t *current_node = list; /* 从头节点开始 */  
    while (current_node->next) { /* 遍历到最后一个节点 */  
        current_node = current_node->next;  
    }  
  
    current_node->next = node; /* 将新节点插入到链表末尾 */

    return list;
}

/*尾部删除节点*/
Link_list_t* Pop_Back(Link_list_t *list)
{
    if (list == NULL || list->next == NULL) {  

        return list; 
    }  
  
    Link_list_t *current_node = list;  
    Link_list_t *prev_node = NULL;  
  
    while (current_node->next) {  /*出去之后为最后一个节点*/
        prev_node = current_node;  
        current_node = current_node->next;  
    }  

    prev_node->next = NULL;  
    Delete_Node(current_node);

    return list;
}

/*头部插入节点*/
Link_list_t* Push_Front(Link_list_t *list,ElemType *elem)
{
    if (list == NULL)
    {
        return NULL;
    }
    Link_list_t *node = Create_Node(elem);/*创建节点*/
    Link_list_t *current_node = list->next;/*首元节点*/

    list->next = node;
    node->next = current_node;

    return list;
}

/*头部删除节点*/
Link_list_t* Pop_Front(Link_list_t *list)
{
    if (list == NULL)
    {
        return NULL;
    }
    Link_list_t *current_node = list->next;/*首元节点*/
    Link_list_t *new_node = current_node->next;/*新的首元节点*/

    Delete_Node(current_node);
    list->next = new_node;
    return list;
}

/*指定位置插入节点*/
Link_list_t* Insert_Node(Link_list_t *list,int position,ElemType *elem)
{
    if (!list)
    {
        return NULL;
    }

    int current_num = get_Node_Num(list);
    if (position < 1 || position > current_num)
    {
        return list;/*位置不合法，插入失败，返回原链表*/
    }

    if (position == 1)
    {
        return Push_Front(list,elem);/*直接头插*/
    }


    int count = 0;
    Link_list_t *node = Create_Node(elem);/*创建新节点*/

    Link_list_t *current_node = list;
    Link_list_t *pervious_node = NULL;

    while (current_node && (count < current_num))
    {
        if (count == position)
        {
            break;
        }

        count ++;
        pervious_node = current_node;
        current_node = current_node->next;
    }
    
    pervious_node->next = node;
    node->next = current_node;

    return list;
}

/*指定位置删除节点*/
Link_list_t* Delete_Node_Anywhere(Link_list_t *list,int position)
{
    if (!list)
    {
        return NULL;
    }

    int current_num = get_Node_Num(list);
    if (position < 1 || position > current_num)
    {
        return list;/*位置不合法，插入失败，返回原链表*/
    }
    
    int count = 0;

    Link_list_t *current_node = list;
    Link_list_t *pervious_node = NULL;
    while (current_node && (count < current_num))
    {
        if (count == position)
        {
            break;
        }

        count ++;
        pervious_node = current_node;
        current_node = current_node->next;
    }
    
    pervious_node->next = current_node->next;
    Delete_Node(current_node);

    return list;
}



